<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar"
      fixed>
      <van-button slot="title"
        round
        size="mini"
        class="searchBtn"
        icon="search"
        to="/search">搜索
      </van-button>
    </van-nav-bar>
    <!-- 频道列表 -->
    <!--  Tab标签页 使用的组件属性说明:
    通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
    通过animated属性可以开启切换标签内容时的转场动画
    通过swipeable属性可以开启滑动切换标签页  -->
    <van-tabs v-model="active"
      animated
      swipeable
      class="channel-tabs">
      <van-tab :title="obj.name"
        v-for="obj in channels"
        :key="obj.id">
        <ArticleList :channels="obj"></ArticleList>
      </van-tab>
      <!-- 占位符 样式涉及到 flex-shrink:0 -->
      <div slot="nav-right"
        class="placeholder"></div>
      <!-- 频道编辑按钮 汉堡导航-->
      <div slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup v-model="isChannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }">
      <ChannelEdit :myChannels="channels"
        :myIndex="active"
        @changeChannel="changeChannel"></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannel } from '@/api/home'
import ArticleList from './components/ArticleList'
import ChannelEdit from './components/ChannelEdit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  // 组件名称
  name: 'HomeIndex',
  // 局部注册的组件
  components: {
    ArticleList,
    ChannelEdit
  },
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  // 计算属性
  computed: {
    ...mapState(['user'])
  },

  mounted () {
    this.initChannels()
  },
  // 组件方法
  methods: {
    // 封装获取用户频道的请求方法
    async initChannels () {
      // 先判断用户是否登录
      if (this.user) { // 已登录
        try {
          const { data: res } = await getUserChannel()
          // console.log(res)
          this.channels = res.data.channels
        } catch (err) {
          console.log('获取用户频道列表失败', err)
        }
      } else { // 未登录
        const localChannels = getItem('TOUTIAO_CHANNEL')
        if (localChannels) { // 如果本地存储有数据
          this.channels = localChannels
        } else { // 如果本地存储没有数据 就再次发送网络请求 获取服务器返回的默认推荐频道列表
          const { data: res } = await getUserChannel()
          // console.log(res)
          this.channels = res.data.channels
        }
      }
    },
    // 汉堡频道按钮中 编辑频道列表后触发过来的函数 改变父组件频道列表的切换
    changeChannel (index, bool) {
      // console.log(index)
      this.active = index
      // 通过active改变频道状态后 关闭弹出层
      this.isChannelEditShow = bool
    }

  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .page-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      max-width: unset;
    }
  }
  .searchBtn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    color: #fff;
    .van-icon-search {
      color: #fff;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      height: 82px;
      top: 92px;
      // Tab标签页设置成固定定位后 切记一定要把左右边界设置为0 否则在flex布局中 导航栏就无法实现左右滑动效果
      left: 0;
      right: 0;
      z-index: 1;
      .van-tab {
        min-width: 200px;
        border-right: 1px solid #edeff3;
        font-size: 30px;
        color: #777777;
      }
      .van-tab--active {
        color: #333333;
      }

      .van-tabs__nav {
        // padding-bottom: 0;
        padding: 0;
      }

      .van-tabs__line {
        bottom: 8px;
        width: 31px !important;
        height: 6px;
        background: #3296fa;
      }
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0px;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
